package com.pc.controller;

import com.pc.entity.Users;
import com.pc.service.MoneyService;
import com.pc.service.RedisService;
import com.pc.service.SendEmailService;
import com.pc.service.UsersService;
import com.pc.utils.Result;
import com.pc.utils.aop.OperLog;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;

@RestController
@RequestMapping("user")
public class UsersController {

    @Resource
    private UsersService usersService;

    @Resource
    private MoneyService moneyService;

    @Resource
    private RedisService redisService;//redis服务

    @Resource
    private SendEmailService sendEmailService;

    @Autowired
    ThreadPoolTaskExecutor applicationTaskExecutor;

    //登录接口
    @PostMapping("/login")
    @OperLog(operModul = "用户模块",operType = "查询",operDesc = "用户登录")
    public Result login(@RequestBody Users users) {
        boolean login = usersService.login(users);
        if (login) {
            return Result.success("登录成功");
        } else {
            return Result.fail("登陆失败");
        }
    }
    //修改密码接口
    @PostMapping("/repwd")
    @OperLog(operModul = "用户模块",operType = "修改",operDesc = "修改用户密码")
    public Result repwd(@RequestBody Users users){
        boolean repwd = usersService.repwd(users);
        if(repwd)
            return Result.success("成功",true);
        else
            return Result.fail("修改失败",false);
    }

    //注册用户
    @PostMapping("/addUser")
    @OperLog(operModul = "用户模块",operType = "增加",operDesc = "注册用户")
    public Result addUser(@RequestBody Users users){

        boolean b = usersService.addUser(users);
        if (b ){
            moneyService.addUserMoney(users.getUsername());
            return Result.success("添加用户成功");
        }
        else
            return Result.fail("添加失败");
    }

    //忘记密码,先把用户名 生成验证码存到redis中 异步优化接口
    @PostMapping("setRedisCodePassword")
    @OperLog(operModul = "用户模块",operType = "修改",operDesc = "用户调用忘记密码接口")
    public Result forgetPassword(@RequestBody Object obj)
            throws ExecutionException, InterruptedException{

        Map map = (Map) obj;
        String username = map.get("username").toString();
        String email = map.get("email").toString();
        //1.生成六位随机数,按照账号验证码存到redis中30秒
        Random random = new Random();
        String code1 = "";
        for (int i = 0; i < 6; i++){
            code1 += random.nextInt(10);
        }
        String code=code1;
        //开始异步
        CompletableFuture<Void> runAsync1 = CompletableFuture.runAsync(() -> {
            redisService.setUserCode(username, code);
        }, applicationTaskExecutor);

        CompletableFuture<Void> runAsync2 = CompletableFuture.runAsync(() -> {
            sendEmailService.sendMail(email,"修改密码验证码",code);
        }, applicationTaskExecutor);

        //等待完成
        CompletableFuture.allOf(runAsync1,runAsync2).get();

        String userCode = redisService.getUserCode(username);

        return Result.success("邮箱验证码存入redis",userCode);
    }
//    @PostMapping("setRedisCodePassword")
//    @OperLog(operModul = "用户模块",operType = "修改",operDesc = "用户调用忘记密码接口")
//    public Result forgetPassword(@RequestBody Object obj)
//            throws ExecutionException, InterruptedException{
//        long start = System.currentTimeMillis();
//
//        Map map = (Map) obj;
//        String username = map.get("username").toString();
//        String email = map.get("email").toString();
//        //1.生成六位随机数,按照账号验证码存到redis中30秒
//        Random random = new Random();
//        String code = "";
//        for (int i = 0; i < 6; i++){
//            code += random.nextInt(10);
//        }
//
//
//        redisService.setUserCode(username, code);
//        sendEmailService.sendMail(email,"修改密码验证码",code);
//        //等待完成
//
//        long end = System.currentTimeMillis();
//        System.out.println("时间"+(end-start));
//        String userCode = redisService.getUserCode(username);
//
//        return Result.success("邮箱验证码存入redis",userCode);
//    }

    @GetMapping("getRedisCodePassword/{username}")
    @OperLog(operModul = "用户模块",operType = "查询 ",operDesc = "用户获取redis中存的验证码")
    public Result getCodePassword(@PathVariable String username){
        String userCode = redisService.getUserCode(username);
        //如果为空说明获取成功
        if (userCode==null)
            return Result.fail("获取失败");
        else
            return Result.success("获取成功",userCode);
    }


}
